package edu.ycxy.poetry.controller;


import com.github.pagehelper.PageInfo;
import edu.ycxy.poetry.entity.Ju;
import edu.ycxy.poetry.service.IJuService;
import edu.ycxy.poetry.vo.AddSentenceVo;
import edu.ycxy.poetry.vo.UpdateSentenceVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author @xin
 * @createDate 2022/3/8
 * @Description
 */
@RestController
@RequestMapping("/ju")
@Slf4j
public class JuController {

    @Autowired
    private IJuService juService;

    //查询名句表中的信息,并对其进行分页操作
    @RequestMapping("/selectListByJu")
    public PageInfo<Ju> selectListJu(Integer pageNum){
        //判断页码是否存在
        if (pageNum == null){
            pageNum = 1;
        }
        // 对查询出来的结果进行分页展示
        final PageInfo<Ju> juPageInfo = juService.selectListJu(pageNum, 50);
        return juPageInfo;
    }

    //根据名句表中的gid查询该名句的作者
    @RequestMapping("/{gid}")
    public String findNameByGid(@PathVariable("gid") Integer gid){
        log.debug("回传的gid为:"+gid);
        return juService.findNameByGid(gid);
    }

    //根据名句的gid查询名句内容
    @RequestMapping("/gid/{gid}/{title}")
    public String findTitleByGid(@PathVariable("gid") Integer gid,@PathVariable("title") String title){
        log.debug("名句的gid为:"+gid);
        return juService.findTitleByGid(gid,title);
    }

    //获取名句的前42条名句
    @RequestMapping("/jus")
    public List<Ju> selectJus(){
        return juService.selectJus();
    }

    @RequestMapping("/addSentence")
    public String addSentences(@RequestBody AddSentenceVo addSentenceVo){
        log.debug("获取到的数据为： " + addSentenceVo);
        String insert = juService.insert(addSentenceVo);
        if (insert.equals("successfully")){
            return "1";
        }else {
            return "2";
        }
    }

    @RequestMapping("/jus/like/{title}")
    public PageInfo<Ju> findListByTileToLike(@PathVariable("title") String title,Integer pageNum){
        if (pageNum == null){
            pageNum = 1;
        }
        PageInfo<Ju> tileToLike = juService.findListByTileToLike(title, pageNum, 10);
        log.debug("获取到的名句信息:" + tileToLike);
        return tileToLike;
    }

    //根据id删除名句信息
    @RequestMapping("/delete/{id}")
    public void deleteJuById(@PathVariable("id") Integer id){
        //log.debug("删除：" + juService.deleteJuById(id));
        juService.deleteJuById(id);
    }

    //根据id向东西赋值到模态框上
    @RequestMapping("/findOneById/{id}")
    public UpdateSentenceVo findOneById(@PathVariable("id") Integer id){
        UpdateSentenceVo oneById = juService.findOneById(id);
        log.debug("获取的值为：" + oneById);
        return oneById;
    }

    //根据id更新信息
    @RequestMapping("/update")
    public Integer updateJuById(@RequestBody UpdateSentenceVo updateSentenceVo){
        log.debug("获取到前端传输的数据 -->：" + updateSentenceVo);
        Integer integer = juService.updateJuById(updateSentenceVo);
        if (integer == 1){
            return 1;
        }else {
            return 2;
        }
    }

}
